package com.nova.edc.service.system.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.system.ElecPriceDao;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.ElecPrice;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.ElecPriceService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.module.filter.query.Filter;
import com.seio.edc.util.StringUtil;

public class ElecPriceServiceImpl implements ElecPriceService {

    @Autowired
    private ElecPriceDao novaElecPriceDao;

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    @Override
    public PageList<ElecPrice> findElecPricePageList(ElecPrice elecPrice,
                                                     Page page, String sidx, String sord) {
        Query<ElecPrice> query = novaElecPriceDao.query()
                .add(Restrictions.eq("Scrapped", 0))
                .addOrder(Order.custom(sord, sidx));
        Filter.addFilterCondition(query, elecPrice);
        return query.pageList(page);
    }

    @Override
    public ElecPrice insert(ElecPrice elecPrice) {
        return novaElecPriceDao.insert(elecPrice);
    }

    @Override
    public int update(ElecPrice elecPrice) {
        return novaElecPriceDao.updateNotNull(elecPrice);
    }

    @Override
    public ElecPrice getById(int id) {
        return novaElecPriceDao.getById(id);
    }

    @Override
    public int delete(int id) {
        ElecPrice elecPrice = novaElecPriceDao.getById(id);
        elecPrice.setScrapped(1);
        return novaElecPriceDao.updateNotNull(elecPrice);
    }

    @Override
    public List<ElecPrice> findElecPriceByUser(int loginId) {
        return novaElecPriceDao.query().add(Restrictions.eq("Scrapped", 0))
                .list();
    }

    @Override
    public boolean isExistElecPriceCode(Integer ePriceCode, Integer codeId) {
        Query<ElecPrice> query = novaElecPriceDao.query()
                .add(Restrictions.eq("t_elecprice.EPriceCode", ePriceCode))
                .add(Restrictions.eq("t_elecprice.Scrapped", 0));
        if (codeId != null && codeId != 0) {
            query.add(Restrictions.ne("t_elecprice.ID", codeId));
        }
        int count = query.list().size();
        return count == 0;
    }

    @Override
    public List<ElecPrice> findElecPriceByCompanyCode(int companyCode) {
        CustomerInfo customerInfo = novaCustomerInfoService
                .getByCompanyCode(companyCode);
        if (customerInfo != null) {
            String[] ElecPriceCodes = StringUtil.isEmpty(customerInfo.getEtype()) ? null
                    : customerInfo.getEtype().split(ModelConstant.SEPARATOR);
            List<ElecPrice> elecPrices = ElecPriceCodes == null
                    || ElecPriceCodes.length == 0 ? null : novaElecPriceDao
                    .query()
                    .add(Restrictions.in("t_elecprice.EPriceCode",
                            Arrays.asList(ElecPriceCodes))).add(Restrictions.eq("t_elecprice.Scrapped", 0)).list();
            return elecPrices;
        } else {
            return null;
        }
    }

    @Override
    public ElecPrice findElecPriceByOneCompanyCode(Integer companyCode) {
        CustomerInfo customerInfo = novaCustomerInfoService
                .getByCompanyCode(companyCode);
        if (customerInfo != null) {
            String[] ElecPriceCodes = StringUtil.isEmpty(customerInfo
                    .getEtype()) ? null : customerInfo.getEtype().split(
                    ModelConstant.SEPARATOR);
            ElecPrice elecPrice = ElecPriceCodes == null
                    || ElecPriceCodes.length == 0 ? null : novaElecPriceDao
                    .query()
                    .add(Restrictions.in("t_elecprice.EPriceCode",
                            Arrays.asList(ElecPriceCodes))).add(Restrictions.eq("t_elecprice.Scrapped", 0)).get();
            return elecPrice;
        } else {
            return null;
        }

    }
}
